﻿using System;
using System.Globalization;
using Cms.Application.Admin.Presenters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Cms.Domain;
using Cms.Application.Admin.Views;
using Moq;

namespace Cms.Tests.Unit.Application.Admin.Presenters
{
    [TestClass]
    public class EditAdministratorPresenterTests : BasePresenterTests
    {
        private Site _site;
        private Administrator _admin;
        private Mock<IEditAdministratorView> _view;
        
        private void Prepare()
        {
            _site = new Site { ID = 1 };
            _admin = new Administrator
            {
                DefaultSite = _site,
                DefaultSystemLocale = "en-US",
                Email = "test@fredium.com",
                Firstname = "Charles",
                Lastname = "Bronson",
                Password = "dontmess",
                Username = "daman",
                DateFormat = "yyyy-MM-dd",
                TimeFormat = "HH:mm",
                Created = new DateTime(2010, 03, 19),
                CurrentSite = _site,
                ShowIntroHelp = false
            };
            AdministratorGroup group = new AdministratorGroup { ID = 1 };
            group.Administrators.Add(_admin);
            _admin.Group = group;
            InitRepositoryFactoryMock();
            AdministratorRepositoryMock.Setup(a => a.GetAdministrator(1)).Returns(_admin);
            _view = new Mock<IEditAdministratorView>();
            _view.SetupAllProperties();
            _view.Setup(v => v.AdministratorID).Returns(1);
            _view.Setup(v => v.Localization).Returns("en-US");
            _view.Setup(v => v.ContextTranslationFile).Returns("Admin/Translations/EditAdministrator.xml");
        }
        
        [TestMethod]
        public void Initialize_EditAdministrator_Success()
        {
            Prepare();
            _view.Setup(v => v.EditedAdministratorID).Returns(1);
            EditAdministratorPresenter presenter = new EditAdministratorPresenter(_view.Object, FactoryMock.Object);
            presenter.Initialize();
            Assert.AreEqual(_admin.Email, _view.Object.Email);
            Assert.AreEqual(_admin.Firstname, _view.Object.Firstname);
            Assert.AreEqual(_admin.Lastname, _view.Object.Lastname);
            Assert.AreEqual(_admin.Password, _view.Object.Password);
            Assert.AreEqual(_admin.Username, _view.Object.Username);
            Assert.AreEqual(_admin.DateFormat, _view.Object.DateFormat);
            Assert.AreEqual(_admin.TimeFormat, _view.Object.TimeFormat);
        }
        
        [TestMethod]
        public void Initialize_NewAdministrator_Success()
        {
            Prepare();
            _view.Setup(v => v.EditedAdministratorID).Returns(-1);
            EditAdministratorPresenter presenter = new EditAdministratorPresenter(_view.Object, FactoryMock.Object);
            presenter.Initialize();
            Assert.AreEqual(1, _view.Object.SelectedAdministratorGroupID);
            Assert.AreEqual(_admin.CurrentSite.ID, _view.Object.SelectedDefaultSiteID);
            Assert.AreEqual(_admin.DefaultSystemLocale, _view.Object.DefaultSystemLocale);
            DateTimeFormatInfo dtfi = new CultureInfo(_admin.DefaultSystemLocale).DateTimeFormat;
            Assert.AreEqual(dtfi.ShortDatePattern, _view.Object.DateFormat);
            Assert.AreEqual(dtfi.ShortTimePattern, _view.Object.TimeFormat);
            Assert.AreEqual(true, _view.Object.ShowIntroHelp);
        }
    }
}
